package com.daohang361.imqq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import com.csu.client.logger.EIMLogger;
import com.csu.client.tools.EIMClientConfig;
import com.csu.message.LoginMessage;
import com.daohang361.imqq.Asycore.QQUser;
import com.daohang361.imqq.resourse.ResourcesManagement;
import com.daohang361.imqq.service.LoginService;
import com.daohang361.imqq.ui.comboBox.ComplexJComboBoxRenderer;
import com.daohang361.imqq.ui.comboBox.JComboBoxItem;
import com.daohang361.imqq.ui.img.Imagecanvas;
import com.daohang361.imqq.ui.trayIcon.EIMTrayIcon;

/**
 * 
 * 登录界面
 */
public class EIMLoginFrame extends BaseFrame{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String ddr="";
    private static EIMLoginFrame instance;
    private String qqAcount;
    private String pwd;
    private String state;
    private LoginMessage  loginMessage;
	public  JOptionPane jOptionPane;
	private static QQUser user;
    
    public static EIMLoginFrame getInstance(){
    	if(instance==null){
    		instance=new EIMLoginFrame();
    	}
    	return instance;
    }
	
    private EIMLoginFrame() {
    	super();
        jOptionPane=new JOptionPane();
        initComponents();
        Thread trayIconThread=new Thread(EIMTrayIcon.getInStance());
        trayIconThread.start();
    }
    public  void changTryIcon(){
    	EIMTrayIcon qqTrayIcon=EIMTrayIcon.getInStance();
    	qqTrayIcon.setFrame(this);
    	qqTrayIcon.setTrayIconState(EIMClientConfig.OffLineTryIcon_Type);
    	qqTrayIcon.showIcon(EIMClientConfig.OffLineTryIcon_Type);
    }                  
    private void initComponents() {
    	this.setResizable(false);
    	this.setAlwaysOnTop(true);
    	ListCellRenderer renderer=new ComplexJComboBoxRenderer();    //JComboBox的渲染器
    	jPanel1 = new javax.swing.JPanel();
        Imagecanvas canvas1=new Imagecanvas("ui/title3.jpg",340,65);
        jPanel1.add(canvas1);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        
        qqPwd_PasswordField = new javax.swing.JPasswordField();
        reg_Label = new javax.swing.JLabel(
        		"<html><a style='TEXT-DECORATION:none' href=\" "  +ddr+ "\">注册新账号</a></html>");
        forget_JLabel = new javax.swing.JLabel(
        		"<html><a style='TEXT-DECORATION:none'  href=\" "  +ddr+ "\">忘记密码</a></html>");
        jLabel5 = new javax.swing.JLabel();
        
        JComboBoxItem state_items[]={
        		new JComboBoxItem(new Font("Helvetica", Font.PLAIN, 10), Color.RED, ResourcesManagement.getImageIcon("ui/online.png",16,16), "上线"),
        		new JComboBoxItem(new Font("Helvetica", Font.PLAIN, 10), Color.RED, ResourcesManagement.getImageIcon("ui/mang.png",16,16), "忙碌"),
        		new JComboBoxItem(new Font("Helvetica", Font.PLAIN, 10), Color.RED, ResourcesManagement.getImageIcon("ui/jingyin.png",16,16), "静音"),
                new JComboBoxItem(new Font("Helvetica", Font.PLAIN, 10), Color.BLUE, ResourcesManagement.getImageIcon("ui/yingshen.png",16,16), "隐身")};
       
        state_ComboBox = new javax.swing.JComboBox(state_items);
        state_ComboBox.setRenderer(renderer);
        
        remeberPwd_CheckBox = new javax.swing.JCheckBox();
        autoLogin_CheckBox = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        set_Button = new javax.swing.JButton();
        login_Button = new javax.swing.JButton();

        JComboBoxItem qqAcount_items[]=
{new JComboBoxItem(
        new Font("Helvetica", Font.PLAIN, 20), Color.RED, ResourcesManagement.getImageIcon("head/19.gif",20,20), "526868457"),
		new JComboBoxItem(new Font("Helvetica", Font.PLAIN, 20), Color.BLUE, ResourcesManagement.getImageIcon("head/16.gif",20,20), "1714114540")};
        qqAcount_ComboBox = new javax.swing.JComboBox(qqAcount_items);
        qqAcount_ComboBox.setRenderer(renderer);
        qqAcount_ComboBox.addItemListener(new   java.awt.event.ItemListener()   {   
            public   void   itemStateChanged(ItemEvent   evt)   {  
            	    int state = evt.getStateChange(); 
            	    if(state==ItemEvent.SELECTED){
            	    	qqAcount=evt.getItem().toString();
            	    	EIMLogger.INFO("选中的qq账号为："+qqAcount);   
            	    }
                 
            }   });
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 335, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 65, Short.MAX_VALUE)
        );

        jLabel1.setText("帐号:");

        jLabel2.setText("密码:");


        reg_Label.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                reg_LabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                reg_LabelMouseEntered(evt);
            }
        });

        forget_JLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                forget_JLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                forget_JLabelMouseEntered(evt);
            }
        });

        jLabel5.setText("状态:");

        remeberPwd_CheckBox.setText("\u8bb0\u4f4f\u5bc6\u7801");//记住密码
        remeberPwd_CheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        remeberPwd_CheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        autoLogin_CheckBox.setText("\u81ea\u52a8\u767b\u5f55");//自动登录
        autoLogin_CheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        autoLogin_CheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        jPanel2.setBackground(java.awt.SystemColor.inactiveCaptionText);
        jPanel2.setForeground(java.awt.SystemColor.inactiveCaptionText);
        set_Button.setText("\u8bbe\u7f6e\u2193");//设置↓
        set_Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                set_ButtonMouseClicked(evt);
            }
        });

        login_Button.setText("\u767b\u5f55");//登录
        login_Button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                login_ButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(set_Button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 177, Short.MAX_VALUE)
                .addComponent(login_Button)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(set_Button)
                    .addComponent(login_Button))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        qqAcount_ComboBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jComboBox1MouseEntered(evt);
            }
        });
        qqAcount_ComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(qqAcount_ComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(state_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(remeberPwd_CheckBox))
                    .addComponent(qqPwd_PasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(autoLogin_CheckBox))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(reg_Label)
                            .addComponent(forget_JLabel))))
                .addContainerGap(25, Short.MAX_VALUE))
//            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1,  javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)//logo与账号之间的空白
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(qqAcount_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(reg_Label))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(qqPwd_PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(forget_JLabel))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(state_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(remeberPwd_CheckBox)
                    .addComponent(autoLogin_CheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void forget_JLabelMouseEntered(java.awt.event.MouseEvent evt) { 
    	forget_JLabel.setBackground(Color.RED);

    }                                          

    private void reg_LabelMouseEntered(java.awt.event.MouseEvent evt) { 
    	reg_Label.setBackground(Color.RED);

    }                                      

    private void forget_JLabelMouseClicked(java.awt.event.MouseEvent evt) {  
    	forget_JLabel.setBackground(Color.RED);

    }                                          

    private void jComboBox1MouseEntered(java.awt.event.MouseEvent evt) {                                        
    	qqAcount_ComboBox.setEditable(true);
    }                                       

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {                                            

    }                                           

    /**
     * 登录界面设置按钮的事件
     * @param evt
     */
    private void set_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	System.out.println("a");
    	EIMNetSetFrame netSetFrame=EIMNetSetFrame.GetInstance();
     	BaseFrame.centerWindow(netSetFrame);
     	netSetFrame.setVisible(true);  
    	this.setEnabled(false);
    	this.setVisible(false);
    }                                       
    /**
     * 登录按钮的事件
     * @param evt
     */
    private void login_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
    	int num=qqAcount_ComboBox.getSelectedIndex();
		qqAcount=qqAcount_ComboBox.getItemAt(num).toString();
    	char[] cPassword=qqPwd_PasswordField.getPassword();
    	pwd=String.valueOf(cPassword);
		 user = new QQUser();
		user.setQq(Integer.parseInt(qqAcount));
		user.setPasswd(pwd);
    	
    	EIMLoginFrame.getInstance().setVisible(false);
    	if(initQQAcount()){
    		 java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                	WaitLoginFrame.qqNumber=user.getQq();
                	WaitLoginFrame waitingFrame=WaitLoginFrame.getInstance();
                   	BaseFrame.leftWindow(waitingFrame);
                   	waitingFrame.changTryIcon();
                   	waitingFrame.setVisible(true);
//                   	waitingFrame.qqNumber=user.getQq();
                     }});
    	   }
    	
    	new LoginService().login(user);
    	System.out.println("登陆");
    }                                         

    private void reg_LabelMouseClicked(java.awt.event.MouseEvent evt) {  
    	System.out.println(evt.getClickCount());
    }                                      
    
    /**
     * 
     *账号密码格式验证
     */
    private boolean initQQAcount(){
	    	if(null==this.qqAcount){
	    		int num=qqAcount_ComboBox.getSelectedIndex();
	    		qqAcount=qqAcount_ComboBox.getItemAt(num).toString();
	    	}
	    	if(qqAcount.equals("")||null==qqAcount){
	        	javax.swing.JOptionPane.showMessageDialog(this,"请输入ID!!..");
	        	return false;
	    	}
    		int state_num=state_ComboBox.getSelectedIndex();
        	state=state_ComboBox.getItemAt(state_num).toString();
        	
        	char[] cPassword=qqPwd_PasswordField.getPassword();
        	pwd=String.valueOf(cPassword);
        	
        	if(pwd.equals("")||pwd==null){
        		javax.swing.JOptionPane.showMessageDialog(this,"请输入密码!!..");
        		return false;
        	}
//    		loginMessage=new LoginMessage();
//			loginMessage.setSrcQQ(qqAcount);
//			loginMessage.setPwd(pwd);
//			loginMessage.setOwerQQ(qqAcount);
//			loginMessage.setState(state);
		    return true;
    }
    /**
     * 
     * 发送登入消息方法
     *
     */
    public void startLoginServer(){
    
    }
    
    // 变量声明 - 不进行修改                     
    private javax.swing.JCheckBox autoLogin_CheckBox;                   //自动登录CheckBox
    private javax.swing.JLabel forget_JLabel;
    private javax.swing.JComboBox qqAcount_ComboBox;                    //QQ账号ComboBox
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton login_Button;                           //登录按钮
    private javax.swing.JPasswordField qqPwd_PasswordField;             //qq密码
    private javax.swing.JLabel reg_Label;
    private javax.swing.JCheckBox remeberPwd_CheckBox;                  //记住密码CheckBox
    private javax.swing.JButton set_Button;                             //设置按钮
    private javax.swing.JComboBox state_ComboBox;                       //状态ComboBox
    // 变量声明结束                   

	public LoginMessage getLoginMessage() {
		return loginMessage;
	}
}
